<?php
/*
 * @Author: 波波
 * @Date: 2025-07-04 22:51:42
 * @Email: guqule2@yeah.net
 * @LastEditors: 波波
 * @LastEditTime: 2025-07-04 23:21:53
 * @Description: 丸子社区-专注分享交流的社区平台，连接你我
 * @Company: 丸子社区
 */


// 处理搜索请求
add_action('wp_ajax_nopriv_wanzicms_search', 'wanzicms_search');
add_action('wp_ajax_wanzicms_search', 'wanzicms_search');

function wanzicms_search()
{
    $query = sanitize_text_field($_GET['s']);

    $args = array(
        's' => $query,
        'posts_per_page' => 10
    );

    $search_query = new WP_Query($args);
    $results = array();

    if ($search_query->have_posts()) {
        while ($search_query->have_posts()) {
            $search_query->the_post();
            $results[] = array(
                'title' => get_the_title(),
                'excerpt' => get_the_excerpt(),
                'link' => get_permalink()
            );
        }
    }

    wp_send_json($results);
    wp_die();
}

function get_first_image_from_content()
{
    $content = get_the_content();
    $first_img = '';
    preg_match_all('/<img[^>]+src=(?:"([^"]*)"|\'([^\']*)\')[^>]*>/i', $content, $matches);
    if (isset($matches[1][0])) {
        $first_img = $matches[1][0];
    }
    return $first_img;
}

function theme_register_nav_menus()
{
    register_nav_menus(array(
        'primary' => __('主导航菜单', 'theme-text-domain'),
        'footer'  => __('页脚菜单', 'theme-text-domain'),
    ));
}
add_action('after_setup_theme', 'theme_register_nav_menus');

// 隐藏 wpadminbar
add_filter('show_admin_bar', '__return_false');

if (wanzi_option('g_replace_gravatar_url_fieldset')['g_replace_gravatar_url'] ?? true) {
    function replace_gravatar_url($avatar)
    {
        $gravatar_server_list = array(
            'geekzu' => 'sdn.geekzu.org',
            'loli' => 'gravatar.loli.net',
            'other' => wanzi_option('g_replace_gravatar_url_fieldset')['g_custom_gravatar_server'] ?? null,
        );
        $gravatar_server = $gravatar_server_list[wanzi_option('g_replace_gravatar_url_fieldset')['g_select_gravatar_server'] ?? 'geekzu'];
        $avatar = str_replace(array('www.gravatar.com', '0.gravatar.com', '1.gravatar.com', '2.gravatar.com', '3.gravatar.com', 'secure.gravatar.com'), $gravatar_server, $avatar);
        $avatar = str_replace('http://', 'https://', $avatar);

        return $avatar;
    }

    add_filter('get_avatar', 'replace_gravatar_url');
    add_filter('get_avatar_url', 'replace_gravatar_url');
}

// 切换经典编辑器
if (wanzi_option('g_classic_editor_switch')) {
    add_filter('use_block_editor_for_post', '__return_false');
}

function wanzicms_enqueue_scripts()
{
    // 注册和加载页脚脚本 (全站)
    wp_enqueue_script(
        'wanzicms-foot',
        get_template_directory_uri() . '/assets/js/foot.js',
        array(),
        THEME_VERSION,
        true
    );

    // 将通用数据传递给JS
    wp_localize_script('wanzicms-foot', 'wanzicms_data', array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'subscribe_url' => get_template_directory_uri() . '/wp_subscribe.php'
    ));

    // 仅在文章详情页加载这些脚本
    if (is_single()) {
        // Enqueue our custom javascript file
        wp_enqueue_script(
            'wanzicms-interactions',
            get_template_directory_uri() . '/assets/js/post-interactions.js',
            [],
            '1.0.0',
            true
        );

        // Pass data from PHP to our script
        wp_localize_script('wanzicms-interactions', 'post_interactions', [
            'ajax_url' => admin_url('admin-ajax.php'),
            'post_id'  => get_the_ID(),
            'nonce'    => wp_create_nonce('wanzicms_interactions_nonce'),
            'is_user_logged_in' => is_user_logged_in(),
            'initial_likes' => (int) get_post_meta(get_the_ID(), '_post_likes_count', true),
            'is_favorited' => is_post_favorited(get_the_ID()),
            'i18n' => [
                'copied' => '已复制链接！',
                'like'   => '点赞',
                'liked'  => '已赞',
                'favorite' => '收藏',
                'favorited' => '已收藏',
            ]
        ]);
    }
}
add_action('wp_enqueue_scripts', 'wanzicms_enqueue_scripts');

function wanzicms_create_content_pages() {
    $pages = array(
        '关于我们' => '',
        '联系我们' => '',
        '服务条款' => '',
        '隐私政策' => ''
    );

    foreach ($pages as $title => $content) {
        if (null == get_page_by_title($title)) {
            wp_insert_post(array(
                'post_type'    => 'page',
                'post_title'   => $title,
                'post_content' => $content,
                'post_status'  => 'publish',
                'post_author'  => 1,
            ));
        }
    }
}
add_action('after_setup_theme', 'wanzicms_create_content_pages');